import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class FlightInfo {

    
    public static void main(String[] args) throws IOException {

        DateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy h:mm a");
        Date date = new Date();

        int end = 0;

        Scanner sc = new Scanner(System.in);

        while (end != -1) {	

            try

            {
            	//runs two shells scripts which grabs the 
            	// Live Airport Data from Dublin Airports Website and puts them into a text file.
            	
              //  Process procArr = Runtime.getRuntime().exec(
                     //   "/home/dj02/bin/flightlistArrivals.sh");

                System.out.println("Updated Flight Arrivals List @ "
                        + dateFormat.format(date));

              //  Process procDep = Runtime.getRuntime().exec(
                   //     "/home/dj02/bin/flightlistDepartures.sh");

                System.out.println("Updated Flight Departures List @ "
                        + dateFormat.format(date));

            }

            catch (Exception e)

            {

                System.out.println(e.getMessage());

                e.printStackTrace();

            }
            
            //String filename = "/home/dj02/bin/flightlistArr.txt";
         //   String filename2 = "/home/dj02/bin/flightlistDepartures.txt";
            String filename = "flightlistArr.txt";
            String filename2 = "flightlistDepartures.txt";

            String searchFor = "";
            BufferedReader in = null;
            BufferedReader in2 = null;
            int choice = 0;

            System.out
                    .println("\n\n1. Arrivals\n2. Departures\n3. Search by Destination, Airline, Flight No.,  Date, or Time.\nPlease choose option 1,2,3:");

            choice = sc.nextInt();

            if (choice == 1) {
                try {
                    System.out.println("What is your flight number??");

                    searchFor = (new BufferedReader(new InputStreamReader(
                            System.in))).readLine();

                    in = new BufferedReader(new FileReader(new File(filename)));
                } catch (IOException e) {
                }
                String flightRow = null;
                int currentLine = 0;
                // this will be set to true if the string was found
                boolean foundString = false;
                while (true) {
                    currentLine++;
                    // get a line of text from the file
                    try {
                        flightRow = in.readLine();
                    } catch (IOException e) {
                        break;
                    }
                    // checks to see if the file ended (in.readLine() returns
                    // null if the end is reached)
                    if (flightRow == null) {
                        break;
                    }
                    if (flightRow.indexOf(searchFor) == -1) {
                        continue;
                    } else {

                        System.out
                                .println("\nYour Flight Arrival Details:\n*************************************"
                                        + "***********************************\n"
                                        + flightRow
                                        + "\n************************************************************************\n");
                        foundString = true;
                        break;
                    }
                }
                if (!foundString)
                    System.out
                            .println("Sorry you entered the wrong flight number.");
                try {
                    in.close();
                }

                catch (IOException ioe) {
                }
            }

            else if (choice == 2) {

                try {

                    System.out.println("What is your flight number??");

                    searchFor = (new BufferedReader(new InputStreamReader(
                            System.in))).readLine();

                    in = new BufferedReader(new FileReader(new File(filename2)));
                } catch (IOException e) {
                }
                String flightRow = null;

                int currentLine = 0;
                // this will be set to true if the string was found
                boolean foundString = false;
                while (true) {
                    currentLine++;
                    // get a line of text from the file
                    try {
                        flightRow = in.readLine();
                    } catch (IOException e) {
                        break;
                    }
                    // checks to see if the file ended (in.readLine() returns
                    // null if the end is reached)
                    if (flightRow == null) {
                        break;
                    }
                    if (flightRow.indexOf(searchFor) == -1) {
                        continue;
                    } else {

                        System.out
                                .println("\nYour Flight Departure Details:\n************************************************************************\n"
                                        + flightRow
                                        + "\n*************************************************************");
                        foundString = true;
                        break;
                    }
                }
                if (!foundString)
                    System.out
                            .println("Sorry you entered the wrong flight number.");
                try {
                    in.close();
                } catch (IOException ioe) {
                }

            }
            ////////////////////////////////////////////////////////////////////
            // /////////////////////////////////////
            else if (choice == 3) {

                try {
                    System.out
                            .println("Please enter Destination/Origin, Airline, Flight No.,  Date, or Time.");

                    searchFor = (new BufferedReader(new InputStreamReader(
                            System.in))).readLine();

                    in = new BufferedReader(new FileReader(new File(filename2)));

                } catch (IOException e) {
                }
                String flightRow = null;
                int currentLine = 0;
                // this will be set to true if the string was found
                boolean foundString = false;
                while (true) {
                    currentLine++;
                    // get a line of text from the file
                    try {
                        flightRow = in.readLine();

                    } catch (IOException e) {
                        break;
                    }
                    // checks to see if the file is at the end
                    if (flightRow == null) {
                        break;
                    }
                    if (flightRow.indexOf(searchFor) == -1) {
                        continue;
                    } else {

                        System.out
                                .println("\tOrigin\t\tFlight No.\t\tTime\t\tStatus        ");
                        System.out
                                .println("\n--------------------------------------------------------------------\n\n"
                                        + flightRow
                                        + "\n************************************************************************\n");

                    }
                }
                if (!foundString)
                    System.out.println("Search Finished...");
                try {
                    in.close();
                }

                catch (IOException ioe) {
                }
            } else if (choice != 1 | choice != 2) {

                System.out.println("Error: 1 or 2 only");
            }
            System.out.println("\nRun again?? 0 or -1 to exit");

        }
        end = sc.nextInt();

        System.out.println("\nExited....");
    }

}